package com.linx.common.web.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.linx.common.core.utils.SpringUtils;
import com.linx.common.web.domain.bo.SearchRequest;
import com.linx.common.web.domain.bo.TimeRangeRequest;
import com.linx.common.web.domain.vo.*;
import com.linx.common.web.properties.HkProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 工具类：封装对海康威视门禁资源的接口调用
 */
@Component
@Slf4j
public  class ArtemisAccessControlUtils {
    private String agreement;
    private String host;
    private String appKey;
    private String appSecret;
    private String artemisPath;


    public ArtemisAccessControlUtils() {
        HkProperties hkProperties = SpringUtils.getBean(HkProperties.class);
        agreement = hkProperties.getAgreement();
        host = hkProperties.getHost();
        appKey = hkProperties.getAppKey();
        appSecret = hkProperties.getAppSecret();
        artemisPath = hkProperties.getArtemisPath();
    }
    /**
     * 发送 POST 请求到指定的接口
     *
     * @param apiPath API 接口路径
     * @param body    请求体
     * @return 响应结果
     * @throws Exception 请求出错时抛出异常
     */
    private  String doPost(String apiPath, String body,Map<String, String> querys,String accet,String contentType,
                           Map<String, String> header) throws Exception {

        agreement="https";
        host="221.213.61.71:38443";
        appKey="27993741";
        appSecret="2rOQNXTW6pdoGhVXH64z";
        artemisPath="/artemis";
        Map<String, String> path = new HashMap<>(2);
        path.put(agreement+"://", artemisPath + apiPath);

        ArtemisConfig.host=host;
        ArtemisConfig.appKey=appKey;
        ArtemisConfig.appSecret=appSecret;
        return ArtemisHttpUtil.doPostStringArtemis( path, body, querys,accet, contentType);
    }

    /**
     * 查询门禁设备列表 v2
     */
    public  ResponseVo searchAccessDevices(SearchRequest searchRequest) throws Exception {
        String apiPath = "/api/resource/v2/acsDevice/search";
        String json = doPost(apiPath, JSON.toJSONString(searchRequest), null, null, "application/json", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json："+jsonObject);
        ResponseVo<DataVo<AcsDeviceVo>> responseVo = JSON.parseObject(json,
            new TypeReference<ResponseVo<DataVo<AcsDeviceVo>>>() {});
        return responseVo;
    }
    /**
     * 查询门禁点列表 v2
     */
    public ResponseVo   searchDoorPoints(SearchRequest searchRequest) throws Exception {
        String apiPath = "/api/resource/v2/door/search";
        String json = doPost(apiPath, JSON.toJSONString(searchRequest), null, null, "application/json", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json："+jsonObject);
        ResponseVo<DataVo<DeviceVo>> responseVo = JSON.parseObject(json, new TypeReference<ResponseVo<DataVo<DeviceVo>>>() {});
        return responseVo;
    }


    /**
     * 增量获取门禁设备数据
     */
    public  ResponseVo getAccessDeviceDataIncrementally(TimeRangeRequest timeRangeRequest) throws Exception {
        String apiPath = "/api/resource/v1/acsDevice/timeRange";
        String json = doPost(apiPath, JSON.toJSONString(timeRangeRequest), null, null, "application/json", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json："+jsonObject);
        ResponseVo<DataVo<AccessDeviceDataIncrementallyVo>> responseVo = JSON.parseObject(json, new TypeReference<ResponseVo<DataVo<AccessDeviceDataIncrementallyVo>>>() {});
        return responseVo;
    }
    /**
     * 增量获取门禁点数据
     */
    public  ResponseVo getDoorDataIncrementally(TimeRangeRequest timeRangeRequest) throws Exception {
        String apiPath = "/api/resource/v1/door/timeRange";
        String json = doPost(apiPath, JSON.toJSONString(timeRangeRequest), null, null, "application/json", null);
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json："+jsonObject);
        ResponseVo<DataVo<DoorDataIncrementallyVo>> responseVo = JSON.parseObject(json, new TypeReference<ResponseVo<DataVo<DoorDataIncrementallyVo>>>() {});
        return responseVo;
    }


    public static void main(String[] args) {

        String apiPath = "/api/resource/v2/acsDevice/search";
        ArtemisAccessControlUtils artemisAccessControlUtils = new ArtemisAccessControlUtils();
        try {
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.setPageNo(1);
            searchRequest.setPageSize(100);

            ResponseVo responseVo = artemisAccessControlUtils.searchAccessDevices(searchRequest);
            System.out.println(responseVo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
